package me.cabbit.digitone.gui;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;

import me.cabbit.digitone.support.Strings;
import me.cabbit.digitone.tones.Screentone;

/**
 * Toolbox for the main window. As with other parts, this class does not solve 
 * anything on itself. Only sets up the layout, adds controls and listeners that
 * call back to the main window
 * 
 * @author Arkaid
 *
 */
class MainWindowToolbox extends JPanel implements ActionListener, ItemListener {
	private static final long serialVersionUID = 2011316774853630591L;

	//------------------------------------------------------------------//
	//--------------------------- properties ---------------------------//
	//------------------------------------------------------------------//

	/** Window this toolbox belongs to */
	protected MainWindow mainWnd;
	
	/** Screentone width */
	protected JSpinner spnWidth;
	
	/** Screentone height */
	protected JSpinner spnHeight;
	
	/** Combobox for the presets */
	protected JComboBox<Preset> cbxPresets;
	
	/** Combobox for the screentones */
	protected JComboBox<Screentone> cbxScreentones;
	
	/** Panel with options for the currently selected screentone 
	 * (uses {@link CardLayout} ) */
	protected JPanel screentoneOptionsContainer;
	
	//------------------------------------------------------------------//
	//----------------------------- methods ----------------------------//
	//------------------------------------------------------------------//

	/**
	 * Constructor. Builds up the toolbox panel and each sub-part
	 *
	 * @param mainWnd Main window this toolbox belongs to
	 */
	public MainWindowToolbox(MainWindow mainWnd) 
	{
		this.mainWnd = mainWnd;
		
		//Set layout and prepare constraints
		this.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(3, 3, 3, 3);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.1; 
		
		//add size panel
		c.gridx = 0; c.gridy = 0; c.weighty = 0;
		this.add(createSizePanel(), c);	
			
		//add screentone select panel
		c.gridx = 0; c.gridy = 1; c.weighty = 1;
		c.fill = GridBagConstraints.BOTH;
		this.add(createScreentonesPanel(), c);

		//add presets panel
		c.gridx = 0; c.gridy = 2; c.weighty = 0;
		this.add(createPresetsPanel(), c);
		
	}
	
	/**
	 * Creates the "Width, Height" mini panel
	 * @return a panel containing the controls
	 */
	private JPanel createSizePanel() 
	{
		JPanel panel = new JPanel();

		//shorthand
		Strings str = this.mainWnd.strings;
		
		//set border
		panel.setBorder(BorderFactory.createTitledBorder(str.get("TOOLBOX_SCREENTONE_SIZE")));
		
		//set layout and constraints
		panel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(3, 3, 3, 3);
		c.fill = GridBagConstraints.HORIZONTAL;

		//add width label and spinner
		c.gridx = 0; c.gridy = 0; c.weightx = 0.1; c.weighty = 1;
		panel.add(new JLabel(str.get("TOOLBOX_SCREENTONE_WIDTH")+":"), c);
		
		c.gridx = 1; c.weightx = 1.0;
		this.spnWidth = new JSpinner();
		panel.add(this.spnWidth, c);
		
		//add height label and spinner
		c.gridx = 2; c.weightx = 0.1;
		panel.add(new JLabel(str.get("TOOLBOX_SCREENTONE_HEIGHT")+":"), c);
		
		c.gridx = 3; c.weightx = 1.0;
		this.spnHeight = new JSpinner();
		panel.add(this.spnHeight, c);

		return panel;
	}
	
	/**
	 * Creates the "Screentone preset" mini panel
	 * @return a panel containing the controls
	 */
	private JPanel createPresetsPanel() 
	{
		JPanel panel = new JPanel();

		//shorthand
		Strings str = this.mainWnd.strings;
		
		//set border
		panel.setBorder(BorderFactory.createTitledBorder(str.get("TOOLBOX_PRESET_TITLE")));
		
		//set layout and constraints
		panel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(3, 3, 3, 3);
		c.fill = GridBagConstraints.HORIZONTAL;

		//add presets combo box
		c.gridx = 0; c.gridy = 0; c.weightx = 1; c.weighty = 1;
		this.cbxPresets = new JComboBox<Preset>();
		this.cbxPresets.addItemListener(this);
		panel.add(this.cbxPresets, c);
		
		//add save button
		c.gridx = 1; c.gridy = 0; c.weightx = 0; c.weighty = 1;
		JButton btn = new JButton();
		btn.setPreferredSize(new Dimension(25, 25));		//set size to fit the icon
		ImageIcon ii = new ImageIcon("res/save.png");		//load the icon and resize it
		btn.setIcon(new ImageIcon(ii.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH)));
		btn.setActionCommand("TOOLBOX_PRESET_SAVE");
		btn.addActionListener(this);
		panel.add(btn, c);
		
		//add delete button
		c.gridx = 2; c.gridy = 0; c.weightx = 0; c.weighty = 1;
		btn = new JButton();
		btn.setPreferredSize(new Dimension(25, 25));		//set size to fit the icon
		ii = new ImageIcon("res/del.png");					//load the icon and resize it
		btn.setIcon(new ImageIcon(ii.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH)));
		btn.setActionCommand("TOOLBOX_PRESET_DELETE");
		btn.addActionListener(this);
		panel.add(btn, c);
		
		return panel;
	}

	/**
	 * Creates the "Screentone selection and options" mini panel
	 * @return a panel containing the controls
	 */
	private JPanel createScreentonesPanel() 
	{
		JPanel panel = new JPanel();

		//shorthand
		Strings str = this.mainWnd.strings;

		//set border
		panel.setBorder(BorderFactory.createTitledBorder(str.get("TOOLBOX_SCREENTONE")));

		//set layout and constraints
		panel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(3, 3, 3, 3);
		c.fill = GridBagConstraints.HORIZONTAL;

		//Screentone combo selector
		c.gridx = 0; c.gridy = 0; c.weightx = 1; c.weighty = 0;
		this.cbxScreentones = new JComboBox<Screentone>();
		this.cbxScreentones.addItemListener(this);
		panel.add(this.cbxScreentones, c);
		
		//Sub-panel for screentone options
		c.gridx = 0; c.gridy = 1; c.weightx = 1; c.weighty = 1;
		c.fill = GridBagConstraints.BOTH;
		this.screentoneOptionsContainer = new JPanel();
		this.screentoneOptionsContainer.setBorder(BorderFactory.createTitledBorder(str.get("TOOLBOX_SCREENTONE_OPTIONS")));
		this.screentoneOptionsContainer.setLayout(new CardLayout());
		panel.add(this.screentoneOptionsContainer, c);
		
		return panel;
	}

	public void addScreentone (Screentone st)
	{
		this.cbxScreentones.addItem(st);
		this.screentoneOptionsContainer.add(st.getPanel(), st.getUID());		
	}
	
	//------------------------------------------------------------------//
	//-------------------------- ActionListener ------------------------//
	//------------------------------------------------------------------//

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		switch (e.getActionCommand())
		{
		case "TOOLBOX_PRESET_SAVE":		this.mainWnd.onPresetSave(); break;
		case "TOOLBOX_PRESET_DELETE":	this.mainWnd.onPresetDelete(); break;
		}
		
	}

	//------------------------------------------------------------------//
	//--------------------------- ItemListener -------------------------//
	//------------------------------------------------------------------//

	@Override
	public void itemStateChanged(ItemEvent e) {
		if (e.getSource() == this.cbxPresets)
		{
			Preset pset = (Preset)e.getItem();
			this.mainWnd.onPresetSelected(pset);
		}
		else 
		{
			Screentone st = (Screentone)e.getItem();
			//switch options display on toolbox
			CardLayout cl = (CardLayout)(this.screentoneOptionsContainer.getLayout());
			cl.show(this.screentoneOptionsContainer, st.getUID());

			this.mainWnd.onScreentoneSelected((Screentone)e.getItem());
		}
	}
}
